package com.xywy.bloodandroid.adapter;

import java.util.List;

import android.content.Context;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.xywy.bloodandroid.R;
import com.xywy.bloodandroid.act.ArticleDetailActivity;
import com.xywy.bloodandroid.bean.Article;
import com.xywy.bloodandroid.util.DateUtil;

/*******************************************
 * @Title: ArticleListAdapter.java
 * @Package com.xywy.bloodandroid.adapter
 * @Description: TODO文章列表adapter
 * @author Benwolf
 * @date 2014-12-26 下午10:06:12
 * @version V1.0
 ********************************************/
public class ArticleListAdapter extends BaseAdapter {
	private List<Article> list;
	private Context context;
	private String dir;
	private String typeid;

	public ArticleListAdapter(List<Article> list, String dir, String typeid,
			Context context) {
		this.list = list;
		this.context = context;
		this.dir = dir;
		this.typeid = typeid;
	}

	public class ViewHolder {
		public TextView title;
		public TextView pubdate;
		public TextView description;
		public LinearLayout ll_article_item;

	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return list.size();
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return position;
	}

	@Override
	public View getView(final int position, View convertView, ViewGroup arg2) {
		// TODO Auto-generated method stub
		System.out.println("getView   " + position);
		ViewHolder holder = null;
		if (convertView == null) {
			convertView = View.inflate(context, R.layout.article_item, null);
			holder = new ViewHolder();
			holder.title = (TextView) convertView.findViewById(R.id.tv_title);
			holder.pubdate = (TextView) convertView
					.findViewById(R.id.tv_pubdate);
			holder.description = (TextView) convertView
					.findViewById(R.id.tv_description);
			holder.ll_article_item = (LinearLayout) convertView
					.findViewById(R.id.ll_article_item);
			convertView.setTag(holder);

		} else {
			holder = (ViewHolder) convertView.getTag();
		}
		holder.title.setText(list.get(position).getTitle().toString());
		holder.pubdate.setText(DateUtil.getData(list.get(position).getPubdate()
				.toString()));
		holder.description.setText(list.get(position).getDescription()
				.toString());

		holder.ll_article_item.setOnClickListener(new OnClickListener() {

			private String id;

			@Override
			public void onClick(View arg0) {
				// TODO Auto-generated method stub
				id = list.get(position).getId().toString();
				System.out.println("******************" + id);
				next();

			}

			private void next() {
				// TODO Auto-generated method stub
				Intent intent = new Intent();
				intent.setClass(context, ArticleDetailActivity.class);
				intent.putExtra("id", id);
				intent.putExtra("dirStr", dir);
				intent.putExtra("typeid", typeid);
				context.startActivity(intent);
			}

		});
		return convertView;

	}

}
